<?php

class autoload {
    
    private static $core;

    public function __construct() {
        self::$core = $this;
    }

    public static function getInstance() {
        return self::$core;
    }
}

function getInstance() {
    return autoload::getInstance();
}

function _autoload($classname) {
    if (strpos($classname, 'app\\') === 0) {
        $filepath = APP_PATH . str_replace('\\', '/', substr($classname, 4)) . PHP_EXT;
    } else {
        $filepath = APP_PATH . $classname . PHP_EXT;
    }

    if (!file_exists($filepath)) {
        throw new \Exception(sprintf('%s loading come out error', $classname));
    }
    return include_once($filepath);
}

spl_autoload_register('_autoload');

_autoload('config/constant');
_autoload('core/loader');
_autoload('core/dbstore');
_autoload('core/router');
_autoload('core/controller');
_autoload('core/output');

$core = new autoload;

$core->loader = new Loader;

$core->config = [
    'db' => $core->loader->config('database'),
];

$core->db = new DBStore;

$core->router = new Router;

return $core;
